package com.woniuxy.hrp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.hrp.entity.AdminDoctorAndTime;
import com.woniuxy.hrp.entity.Application;
import com.woniuxy.hrp.entity.Department;
import com.woniuxy.hrp.entity.DoctorAndTime;
import com.woniuxy.hrp.provider.DepartmentSqlProvider;
import com.woniuxy.hrp.provider.DoctorAndTimeSqlProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DoctorAndTimeMapper extends BaseMapper<DoctorAndTime> {
    @Insert("insert into hrp_doctor_time values (null,#{doctorId},#{arrangeTimeId},null,#{num},#{status})")
    void myinsert(AdminDoctorAndTime doctorAndTime);

    @Delete("delete from hrp_doctor_time where doctor_id=#{doctorid}")
    void deletebydoc(int doctorid);
    DoctorAndTime seleById(int id);

    //修改值班状态
    @Update("update hrp_doctor_time set status='n' where id=#{id}")
    void updataStatus(int id);

    //修改排班信息
    @UpdateProvider(type = DoctorAndTimeSqlProvider.class,method = "updateDTByCondition")
    void updateByCondition(AdminDoctorAndTime adminDoctorAndTime);

    //查询医生id排班信息
    @Select("select *from hrp_doctor_time where doctor_id=#{doctorid}")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "num",property = "num"),
            @Result(column = "status",property = "status"),
            @Result(column = "arrangetime_id",property = "arrangeTime",one = @One(select = "com.woniuxy.hrp.mapper.ArrangeTimeMapper.findById"))
    })
    List<DoctorAndTime> findById(int doctorid);

    //查询主键id排班信息
    @Select("select *from hrp_doctor_time where id=#{id}")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "num",property = "num"),
            @Result(column = "status",property = "status"),
            @Result(column = "arrangetime_id",property = "arrangeTime",one = @One(select = "com.woniuxy.hrp.mapper.ArrangeTimeMapper.findById"))
    })
    DoctorAndTime findByFid(int id);

    //医生调班申请
    @Update("update hrp_doctor_time set status='a',newarrangetime_id=#{newid} where id=#{mid}")
    void applyfor(Application application);

    //查询调班申请
    @Select("select *from hrp_doctor_time where status='a'")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "doctor_id",property = "doctor",one = @One(select = "com.woniuxy.hrp.mapper.DoctorMapper.selectById")),
            @Result(column = "arrangetime_id",property = "arrangeTime",one = @One(select = "com.woniuxy.hrp.mapper.ArrangeTimeMapper.findById")),
            @Result(column = "newarrangetime_id",property = "newarrangeTime",one = @One(select = "com.woniuxy.hrp.mapper.ArrangeTimeMapper.findById"))
    })
    List<DoctorAndTime> findApplyfor();

    //根据主键id查询调班信息
    @Select("select *from hrp_doctor_time where id=#{id}")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "arrangetime_id",property = "arrangeTimeId"),
            @Result(column = "newarrangetime_id",property = "newarrangeTimeId")
    })
    AdminDoctorAndTime findApplyforinfo(int id);

    //同意调班
    @Update("update hrp_doctor_time set status='y',arrangetime_id=#{arrangeTimeId} where id=#{id}")
    void agree(AdminDoctorAndTime adminDoctorAndTime);

    @Select("select* from hrp_doctor_time where id=#{id}")
    @Results(value = {
            @Result( column = "id",property = "id"),
            @Result( column = "doctor_id",property = "doctor.id"),
            @Result(column = "doctor_id",property = "doctor",one = @One(select = "com.woniuxy.hrp.mapper.DoctorMapper.selectById"))
    })
    DoctorAndTime getById(int id);
    @Select("select * from hrp_doctor_time where doctor_id=#{doctorid}")
    List<DoctorAndTime> selectbydoctorid(int doctorid);


    @Update("update hrp_doctor_time set num=#{num} where id=#{id}")
    void updateDoctorAndTime(DoctorAndTime doctorAndTime);

    @Select("select * from hrp_doctor_time where doctor_id=#{doctorid}")
    @Results(value = {
            @Result( column = "id",property = "id"),
            @Result(column = "id",property = "reservations",one = @One(select = "com.woniuxy.hrp.mapper.ReservationMapper.selectBydoctorTimeId"))
    })
    List<DoctorAndTime> selectPatientsbydoctorid(int doctorid);

}
